package com.pj.h5;

import com.pj.sso.SsoRequestUtil;
import com.pj.util.AjaxJson;
import com.pj.util.MyHttpSessionHolder;
import com.pj.util.SaCheckTicketResult;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 前后台分离架构下集成SSO所需的代码 
 * @author click33
 *
 */
@RestController
public class H5Controller {

	// 返回SSO认证中心登录地址 （前后台分离环境下专用）
	@RequestMapping("/sso/getSsoAuthUrl")
	public AjaxJson getSsoAuthUrl(String clientLoginUrl) {
		String serverAuthUrl = SsoRequestUtil.buildServerAuthUrl(clientLoginUrl);
		return AjaxJson.getSuccessData(serverAuthUrl);
	}
	
	// 根据ticket进行登录（前后台分离环境下专用）
	@RequestMapping("/sso/doLoginByTicket")
	public AjaxJson doLoginByTicket(String ticket, HttpServletRequest request) {
		HttpSession session = MyHttpSessionHolder.getSession(request);
		SaCheckTicketResult ctr = SsoRequestUtil.checkTicket(ticket, request, "/sso/doLoginByTicket");
		session.setAttribute("userId", ctr.loginId);
		session.setAttribute("user", SsoRequestUtil.getUserInfo(ctr.loginId));
		return AjaxJson.getSuccessData(session.getId());
	}
	
}
